﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine(DateTime.Now.ToString("yyyyMMddHHmmssff"));//记录ID号
            
            //GetInfo();
            //Test3();
            //TestXmlBytes();

            //TestNull t1 = null;
            //Console.WriteLine(t1?.ToString() + "1111");
            //Socket client = null;
            //Console.WriteLine(client?.RemoteEndPoint.ToString() + "**1111");

            TestList();
            Console.Read();
        }

        static async void T1()
        {
            Task task = AsyncMethod();
            task.Wait(); // 或者 task.GetAwaiter().GetResult();
        }

        public static async Task AsyncMethod()
        {
            // 异步操作
            await Task.Delay(1000);
        }

        private static void  TestList()
        {
            List<string> list = new List<string>();
            list.Add("1");
            list.Add("1");
            list.Add("1");
            foreach (var item in list)
            {
                Console.WriteLine(item  + ": " + list.Select(v => v == item).Count()+ "个");
            }
            list.RemoveAll(v=>v=="1");
            list.Remove("0");

            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("1", "100");
            dic.Add("2", "200");
            dic.Add("3", "300");
            dic.Remove("4");
            Console.WriteLine(list.Count() + "个");
        }

        class TestNull
        {
            public int MyProperty { get; set; }
        }

        static void TestUpPic()
        {
            string picfullname = @"H:\超速取证\Test\20160423125359_鲁QR0H80_小型汽车_120_141_青兰高速公路121KM_济南方向_1.jpg";
            MemoryStream ms1 = new MemoryStream();
            Bitmap bmp1 = new Bitmap(picfullname);
            bmp1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
            bmp1.Dispose();
            byte[] zp01 = new byte[(int)ms1.Length];
            zp01 = ms1.ToArray();
            ms1.Dispose();
            string str_pic1 = Convert.ToBase64String(zp01);
            byte [] aa = Encoding.UTF8.GetBytes(str_pic1);

            string base64 = Encoding.UTF8.GetString(aa);

        }
        static void StrToHex(string Msg)
        {
            string str = "";
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(Msg);

                for (int i = 0; i < bytes.Length; i++)
                {
                    str += string.Format("{0:X}", bytes[i]);//CDEE413132333435
                }
            }
            catch (Exception ex)
            {
            }
        }
        static void Test3()
        {
            Console.WriteLine(1.ToString("D8"));

            Console.WriteLine(2.ToString().PadLeft(8,'0'));
        }

        static void Test2()
        {
            byte[] bytes = { 0x00, 0x00, 0x01, 0xF5 }; // 示例字节数组
            int number;

            // 使用小端字节顺序转换
            number = BitConverter.ToInt32(bytes, 0);

            //Console.WriteLine(number);501
            //000001F5
            Console.WriteLine(501.ToString("X"));
            byte[] b501 = HexstrToHexByte(501.ToString("X"));
            //315
            //for (int i = 0; i < b501.Length; i++)
            //{
            //    Console.Write(b501[i] + " ");
            //}

            byte[] bytes2 = { 0x00, 0x00, 0x01, 0xF5 };
            string hexString = BitConverter.ToString(bytes).Replace("-", "");
            int code = Convert.ToInt32(hexString, 16);
            //Console.WriteLine(hexString); // 输出: 000001F5
            //Console.WriteLine(code); // 输出: 501


            byte[] xmlMsgLength = {0x00,0x00,0x01,0xF6 };//xml长度
            string hexString2 = BitConverter.ToString(xmlMsgLength).Replace("-", "");
            int xmlLength = Convert.ToInt32(hexString2, 16);
            Console.WriteLine(xmlLength); // 输出: 502
            

        }

        static void TestXmlBytes()
        {
             //包头 + 数据报文长度 + 协议版本 + 命令码 + 数据内容 + 包尾
            //string data = "77aa77aa 00000000 00000002 00000076 xml 77ab77ab";
            string strXml = @"<?xml version=""1.0"" ?>
            <Response>
                <CamID>50023100051320000504</CamID>
                <RecordID>1900</RecordID>
                <Result>0</Result>
                <ReqCmdID>118</ReqCmdID>
                <DBRecordID>3784</DBRecordID>
            </Response>";
            
            byte[] responseInfo = Encoding.UTF8.GetBytes(strXml);//1、车辆信息Xml

            byte[] dataHead = new byte[4];
            dataHead = HexstrToHexByte("77aa77aa");//包头-16进制格式字符串

            byte[] dataPackageLength = new byte[4];
            string hex = (responseInfo.Length + 8).ToString("X").PadLeft(8, '0');
            byte[] dLen = HexstrToHexByte(hex);//数据报文长度, 包长度之后到包尾之前的所有字段的长度-16进制格式字符串
            Buffer.BlockCopy(dLen, 0, dataPackageLength, 0, dLen.Length);
            string hexString2 = BitConverter.ToString(dataPackageLength).Replace("-", "");


            byte[] dataVersion = new byte[4];
            dataVersion = HexstrToHexByte("00000002");//版本2，其中的XML数据采用XML方式描述-16进制格式字符串

            byte[] dataCommandCode = new byte[4];
            dataCommandCode = HexstrToHexByte("00000076");//命令码118-16进制格式字符串

            byte[] dataTail = HexstrToHexByte("77ab77ab");//包尾

            byte[] sendDataAll = dataHead.Concat(dataPackageLength).Concat(dataVersion).Concat(dataCommandCode).Concat(responseInfo).Concat(dataTail).ToArray();

            string hexString = BitConverter.ToString(sendDataAll).Replace("-", "");
            Console.WriteLine(hexString);
        }


        private static byte[] HexstrToHexByte(string hexString)//2019.03.27： 16进制字符串转化为字节数组
        {
            byte[] returnBytes_yichang = new byte[2];
            try
            {
                hexString = hexString.Replace(" ", "");
                if ((hexString.Length % 2) != 0)
                    hexString = hexString.Insert(hexString.Length - 1, 0.ToString());
                byte[] returnBytes = new byte[hexString.Length / 2];
                for (int i = 0; i < returnBytes.Length; i++)
                    returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
                return returnBytes;
            }
            catch (Exception ex)
            {
                return returnBytes_yichang;
            }
        }

        static void Test1()
        {
            string len = "77aa77aa250166242169051320040001k12345678912  77ab77ab";
            Console.WriteLine(len.Length);
        }

        static void Test()
        {
            string strId = @"<?xml version=""1.0"" encoding=""utf-8""?>
<Response>
 <CamID>卡口相机编号卡口编号</CamID>
 <RecordID>记录ID号</RecordID>
 <!--可选字段-->
 <Result>错误码</Result>
 <ReqCmdID>请求命令码</ReqCmdID>
 <DBRecordID>内部使用，数据库记录</DBRecordID>
</Response>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(strId);

            XmlNode node = xmlDoc.SelectSingleNode("/Response/Result");
            if (node != null)
            {
                string value = node.InnerText;
                Console.WriteLine("节点element1的值是：" + value);
            }

            Console.WriteLine(strId);
        }
        static string returnvalue_code ="",return_message = "";
        static string strUrlHttp = "http://localhost:31154/Api/Values/PostMethod";

        private static void GetInfo()
        {
            IllegalDataModel dataModel = new IllegalDataModel();
            dataModel.hxbbh = "001";//--集指平台设备编号
            dataModel.rksj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//入库时间
            dataModel.wfsj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//违法时间
            dataModel.wfdm = "6666";//违法代码
            dataModel.wfdz = "违法地址1";//违法地址
            dataModel.hphm = "苏L291P9";//车牌号码
            dataModel.cllx = "卡车";//车辆类型
            dataModel.hpzl = "蓝";//车牌种类
            dataModel.fxlx = "0";//方向类型
            dataModel.tpsl = "2";//图片数量
            dataModel.qjtplj = "全景图片(base64)";// 全景图片(base64)
            dataModel.tztplj = "特征图片(base64)";// 特征图片(base64)
            dataModel.dsplj = "D:\\dsp";
            Console.WriteLine(Dataup(dataModel, "picfullname1", "picfullname2"));
        }

        private static string Dataup(IllegalDataModel illegalDataModel, string picfullname1, string picfullname2)
        {
            try
            {
                object returnData = HttpRequestHelper.Post(strUrlHttp, illegalDataModel);
                ReturnDataModel data = JsonConvert.DeserializeObject<ReturnDataModel>(returnData.ToString());

                if (data != null)
                {
                    if (data.code == "200")
                    {
                        
                    }
                    returnvalue_code = data.code + ";" + picfullname1 + "$" + picfullname2;
                    return_message = data.message;
                }
                return returnvalue_code;
            }
            catch (Exception ex)
            {
                returnvalue_code = "网络不通" + ";" + picfullname1 + "$" + picfullname2;
                return returnvalue_code;
            }
        }
    }
}
